<?php

Class Cvest extends Connect{
    
    
    static $action = array(); 
    static $type = array();
    static $all = array();   
    
    function __construct(){
        if( count(Cvest::$action) == 0 ){
            Cvest::$action['buy'] = "купить";
            Cvest::$action['sell'] = "продать";
            Cvest::$action['brew'] = "сварить";
            Cvest::$action['present'] = "подарить";
            Cvest::$action['use'] = "применить";
            
            Cvest::$type['potion'] = "эликсир";
            Cvest::$type['ingredient'] = "ингредиент";
            Cvest::$type['clothing'] = "одежда";
            Cvest::$type['artifact'] = "артифакт";
            #Cvest::$type['mortar'] = "ступка";
            #Cvest::$type['boiler'] = "котел";
            #Cvest::$type['podkova'] = "часы";
        }
    }
    
    function save($data){
        $sql = "UPDATE `".PREFIX_STATIC."cvest` SET `name` = '".$data['name']."', 
                                                    `descript` = '".$data['descript']."', 
                                                    `level` = '".$data['level']."',  
                                                    `practic` = '".@$data['practic']."',
                                                    `money1` = '".$data['money1']."', 
                                                    `money2` = '".$data['money2']."',                                                     
                                                    `image` = '".str_replace(MEDIA_URL, '', $data['image']) ."'
                                                    WHERE `id` = '".$data['id']."'  "; 
        if(!DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        } 
        $del = "DELETE FROM `".PREFIX_STATIC."cvest` WHERE 
                `parent` = '".$data['id']."'";
        if(!DBConnect::query($del)){
            $this->_error(mysql_error());
            return false;
        } 
        if(count($data['task']) > 0){
            return $this->insertTask($data['id'], $data['task']);    
        }                                                
        
    }
    
    function add($data){
        $sql = "INSERT INTO `".PREFIX_STATIC."cvest` (`name`, `descript`, `level`, `money1`, `money2`, `practic`, `image`)
                VALUES
                ('".$data['name']."', '".$data['descript']."', 
                '".$data['level']."', '".$data['money1']."',
                '".$data['money2']."', '".$data['practic']."',
                '".str_replace(MEDIA_URL, '', $data['image'])."')";
        #return $data['task'];
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        $parent = mysql_insert_id();
        if(count($data['task']) > 0){
            if( !$this->insertTask($parent, $data['task']) ) {
                return false; 
            }
        }            
        return $parent;
    }
    
    function delete($parent){
        $sql = "DELETE FROM `".PREFIX_STATIC."cvest` WHERE `id` = '".$parent."' OR `parent` = '".$parent."' ";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    }
    
    function insertTask($parent, $tascs){
        $sql = "INSERT INTO `".PREFIX_STATIC."cvest` (`id`, `parent`, `action`, `type`, `good`, `count`)
                VALUES ";
        $i = 1;
        foreach($tascs as $key => $val){
            $sql .= "( '". ( ( intval($val['idTask']) > 0 )?  intval($val['idTask']): null  ) ."', '".$parent."',  '".$val['action']."',  '".$val['type']."', '".$val['good']."', '".$val['count']."' )" ;
            $sql .= ($i < count($tascs))? ", " : "" ;
            $i++;
        }
        #_echo($sql);
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    }
    
    function getAll($reload = false){
        #_echo($_SESSION['cvest']);
        #if(is_array($_SESSION['cvest']) && count($_SESSION['cvest']) > 0 && !$reload) return;
        if(is_array(Cvest::$all) && count(Cvest::$all) > 0 && !$reload) return;
        
        $sql = "SELECT 
                cvest.id as id,
                cvest.name as name,
                cvest.descript as descript,
                cvest.level as level,
                cvest.money1 as money1,
                cvest.money2 as money2,
                cvest.practic as practic,
                cvest.image as image,
                
                task.id as taskid,
                task.action as action,
                task.type as type,
                task.good as good,
                task.count as count
                   
                FROM 
                (SELECT * FROM `".PREFIX_STATIC."cvest` WHERE `parent` = '0' ) cvest
                LEFT JOIN
                (SELECT * FROM `".PREFIX_STATIC."cvest` WHERE `parent` != '0') task ON cvest.id = task.parent ";
        #_echo($sql);
        if(!$result = DBConnect::query($sql, 'cvest')){
            $this->_error(mysql_error());
            return false;
        }
        while($arr = DBConnect::getOne($result, 'cvest')){
            #return $arr;
            if( !isset(Cvest::$all[$arr['id']]) ){
                Cvest::$all[$arr['id']] = array();
                Cvest::$all[$arr['id']]['name'] = $arr['name']; 
                Cvest::$all[$arr['id']]['id'] = $arr['id'];
                Cvest::$all[$arr['id']]['descript'] = $arr['descript'];
                Cvest::$all[$arr['id']]['level'] = $arr['level'];
                Cvest::$all[$arr['id']]['money1'] = $arr['money1'];
                Cvest::$all[$arr['id']]['money2'] = $arr['money2'];
                Cvest::$all[$arr['id']]['practic'] = $arr['practic'];
                Cvest::$all[$arr['id']]['image'] = ( !Pet::is_url($arr['image']) ? MEDIA_URL.$arr['image'] : $arr['image']  ) ;
                Cvest::$all[$arr['id']]['task'] = array();                
            }
            Cvest::$all[$arr['id']]['task'][$arr['taskid']] = array();                              
            Cvest::$all[$arr['id']]['task'][$arr['taskid']]['id'] = $arr['taskid'];
            Cvest::$all[$arr['id']]['task'][$arr['taskid']]['action']  = $arr['action'];
            Cvest::$all[$arr['id']]['task'][$arr['taskid']]['type']  = $arr['type'];
            Cvest::$all[$arr['id']]['task'][$arr['taskid']]['good']  = $arr['good'];            
            Cvest::$all[$arr['id']]['task'][$arr['taskid']]['count']  = $arr['count'];
        }
        #$_SESSION['cvest'] = Cvest::$all; 
    }
    
    function get ($level){
        $this->getAll();
        $arr = array();
        foreach(Cvest::$all as $key => $val){
        #foreach($_SESSION['cvest'] as $key => $val){
            if($val['level'] <= $level){
                $arr[] = $val;
            }
        }
        return $arr;
        
    }
}
?>